package com.recruit.servlet;

import com.recruit.entity.User;
import com.recruit.service.UpdateUserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserUpdateServlet",urlPatterns = "/updateUser")
public class UserUpdateServlet extends HttpServlet {
    private UpdateUserService updateUserService = new UpdateUserService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取修改数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        Integer age = Integer.valueOf(request.getParameter("age"));
        //获取性别
        String[] values = request.getParameterValues("sex");
        String sex = values[0];
        //封装对象User
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setPhone(phone);
        user.setAge(age);
        user.setSex(sex);
        //调用service层
        int i = updateUserService.updateUserInfo(user);
        if (i>0){
            request.setAttribute("updateUserInfo","信息修改成功");
        }else {
            request.setAttribute("updateUserInfo","信息修改失败，请联系管理员！");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
